package lumis.service.image;

import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.dao.hibernate.GenericHibernateManualUpdateDao;
import lumis.portal.dao.jdbc.ITransactionJdbc;

import lumis.util.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;

/**
 * @version $Revision: 8732 $ $Date: 2008-01-08 12:00:58 -0200 (Tue, 08 Jan 2008) $
 * @since 4.0.0
 */
class ImageDaoHib extends GenericHibernateManualUpdateDao<ImageConfig, String> implements IImageDao
{
	public ImageDaoHib()
	{
		super(ImageConfig.class);
	}	
	
	protected String getIdentifier(ImageConfig bean)
	{
		return bean.getId();
	}
	
	public boolean delete(String imageId, ITransaction transaction) throws PortalException
	{
		return super.delete(imageId, transaction);
	}
	
	public ImageConfig get(String imageId, ITransaction transaction) throws PortalException
	{
		return super.get(imageId, transaction);
	}
	
	public void update(ImageConfig imageConfig, ITransaction transaction) throws PortalException
	{
		super.update(imageConfig, transaction);
	}

	public Collection<String> getIdsByParentFolderAndType(String serviceInstanceId, String folderId, Integer type, ITransaction transaction) throws PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();

			StringBuilder sql = new StringBuilder(200);
			sql.append("select lum_Image.id from lum_Image INNER JOIN " +
					"lum_ContentVersion ON lum_Image.id = lum_ContentVersion.itemId INNER JOIN " +
					"lum_ContentLocale ON lum_ContentVersion.contentLocaleId = lum_ContentLocale.id INNER JOIN " +
					"lum_Content ON lum_ContentLocale.contentId = lum_Content.id " +
					"where lum_Content.serviceInstanceId = ? and ");
			if (folderId != null)				
				sql.append("lum_Image.parentFolder = ?");
			else
				sql.append("lum_Image.parentFolder IS NULL");
			if (type != null)
				sql.append(" and lum_Image.type = " + type);
			PreparedStatement statement = connection.prepareStatement(sql.toString());
			try
			{
				statement.setString(1, serviceInstanceId);
				if (folderId != null)					
					statement.setString(2, folderId);
				Collection<String> returnValues = JdbcUtil.fillListString(statement, "id");
				return returnValues;
			}
			finally
			{
				statement.close();
			}
		}
		catch (SQLException e)
		{
			throw new UnexpectedException(e);
		}		
	}
	
	public Collection<String> getFolderIdsByParentAndTitle(String serviceInstanceId, String parentFolderId, String folderName, ITransaction transaction) throws PortalException
	{
		try
		{
			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();
			
			String sql = "SELECT id FROM lum_Image " +
					"lum_ContentVersion ON lum_Image.id = lum_ContentVersion.itemId INNER JOIN " +
					"lum_ContentLocale ON lum_ContentVersion.contentLocaleId = lum_ContentLocale.id INNER JOIN " +
					"lum_Content ON lum_ContentLocale.contentId = lum_Content.id " +
					"WHERE lum_Image.type = " + ImageConfig.TYPE_FOLDER + 
					" AND lum_Image.title = ? AND lum_Content.serviceInstanceId = ? AND lum_Image.parentFolder ";
			if (parentFolderId != null)
				sql += "= ?";
			else
				sql += "IS NULL";
			PreparedStatement statement = connection.prepareStatement(sql);
			try
			{
				statement.setString(1, folderName);
				statement.setString(2, serviceInstanceId);
				if (parentFolderId != null)					
					statement.setString(3, parentFolderId);
				return JdbcUtil.fillListString(statement, "id");
			}
			finally
			{
				statement.close();
			}
		}
		catch (SQLException e)
		{
			throw new UnexpectedException(e);
		}				
	}
}
